package com.sanji.sjmallmodule.phoneshell;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sanji.sjmallapp.MallApplication;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.image.GlideManager;
import com.sanji.sjmallmodule.order.ShopcarAdapter;
import com.sanji.sjmallutils.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 定制购物车
 * Created by ZSF on 17/9/12.
 */

public class ShellShopCarAdapter extends BaseAdapter {

    private Context mCtx;
    private List<ShellCartBean> cartList = new ArrayList<>();
    private CBoxCheckListener checkListener;


    public ShellShopCarAdapter(Context context,List<ShellCartBean> list){
        this.mCtx = context;
        this.cartList = list;
    }

    public void notify(List<ShellCartBean> list){
        this.cartList = list;
        this.notifyDataSetChanged();
    }

    public void setCBoxCheckListener(CBoxCheckListener listener) {
        this.checkListener = listener;
    }

    @Override
    public int getCount() {
        return cartList.size();
    }

    @Override
    public ShellCartBean getItem(int i) {
        return cartList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ShopcarHolder holder;
        if (view == null){
            view = LayoutInflater.from(mCtx).inflate(R.layout.item_adapter_shell_shopcar,null);
            holder = new ShopcarHolder(view);
            int imgWidth = MallApplication.myShare.getInt(ConfigConts.ScreenWidth,800) / 3 ;
            holder.kkmoveWeb.setLayoutParams(new FrameLayout.LayoutParams(imgWidth,imgWidth));
            WebSettings settings = holder.kkmoveWeb.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
            holder.kkmoveWeb.setWebViewClient(new WebViewClient());
            view.setTag(holder);
        }else{
            holder = (ShopcarHolder) view.getTag();
        }

        ShellCartBean cart = getItem(i);
        if (cart!=null){
            holder.kkmoveWeb.loadUrl(cart.getShowView());
            holder.numTv.setText(cart.getOrderNum());
            holder.brandTv.setText("品牌-型号：" + cart.getCategoryName());
            holder.countTv.setText("数量：" + cart.getQuantity());
            holder.priceTv.setText(Tools.divide(cart.getSalePrice())+"元");
            holder.userNameTv.setText("终端客户姓名："+cart.getUsername());
            holder.userPhoneTv.setText("终端客户电话："+cart.getUserMobile());
            holder.titleCb.setChecked(cart.isSelected());
            holder.titleCb.setTag(i);
            holder.titleCb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (int) v.getTag();
                    ShellCartBean cart = getItem(pos);
                    boolean selct = cart.isSelected();
                    cart.setSelected(!selct);
                    notifyDataSetChanged();

                    checkListener.cbCheck(pos);
                }
            });
            holder.deltTv.setTag(i);
            holder.deltTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (int) v.getTag();
                    checkListener.deltItem(pos);
                }
            });
            holder.editTv.setTag(i);
            holder.editTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (int) v.getTag();
                    checkListener.editKKMove(pos);
                }
            });
        }
        return view;
    }

    class ShopcarHolder{
        @BindView(R.id.item_shell_shopcar_kkmove_web)WebView kkmoveWeb;
        @BindView(R.id.item_shell_shopcar_title_cb)CheckBox titleCb;
        @BindView(R.id.item_shell_shopcar_no_tv)TextView numTv;
        @BindView(R.id.item_shell_shopcar_delt_tv)TextView deltTv;
        @BindView(R.id.item_shell_shopcar_brand_tv)TextView brandTv;
        @BindView(R.id.item_shell_shopcar_count_tv)TextView countTv;
        @BindView(R.id.item_shell_shopcar_price_value_tv)TextView priceTv;
        @BindView(R.id.item_shell_shopcar_user_name_tv)TextView userNameTv;
        @BindView(R.id.item_shell_shopcar_user_phone_tv)TextView userPhoneTv;
        @BindView(R.id.item_shell_shopcar_kkmove_edit_tv)TextView editTv;

        public ShopcarHolder(View view){
            ButterKnife.bind(this,view);
        }
    }

    interface CBoxCheckListener{
        void cbCheck(int pos);
        void deltItem(int pos);
        void editKKMove(int pos);
    }


    public void updateItemView(ListView listView, int pos,String showUrl){
        int visiblePosition = listView.getFirstVisiblePosition();
        int index = pos - visiblePosition;
        if (index>=0){
            View view = listView.getChildAt(index);
            //得到要更新的item的view
            ShopcarHolder holder = (ShopcarHolder) view.getTag();
            //刷新webview
            holder.kkmoveWeb.loadUrl(showUrl);

        }
    }
}
